<?php

namespace app\common\model;

use think\Model;
use think\Db;
class Product extends Model
{
    protected $resultSetType = '';
    // 追加属性
    protected $append = [
        'status_text',
    ];
    public static function get_list($page,$keywords,$car_id)
    {
        $where = [];
        $wheres='';
        if($keywords)
        {
            $where['p.title']=['like','%'.$keywords.'%'];
        }
        if($car_id)
        {
            $wheres="FIND_IN_SET(".$car_id.",car_ids)";
            $where['status']=3;
        }
        $field = "p.id,p.title,p.address,p.manager,p.contact_tel,s.store_name,s.name,p.status";
        $order = "p.status asc";
        $count = self::alias('p')->join('store s','p.store_id=s.id','left')->where($where)->where($wheres)->count();
        $limit = 20;
        $start = ($page - 1) * $limit;
        $total = ceil($count / $limit);
        $list = self::alias('p')->join('store s','p.store_id=s.id','left')->where($where)->where($wheres)->field($field)->order($order)->limit($start, $limit)->select();
    
        $data = [
            'total'=>$total,
            'list'=>$list
        ];
        return $data;
    }

    public static function get_info($id)
    {
        $where['id']=$id;
        $info = self::where($where)->field("*")->find();
        //图片
        $images=[];
        if($info['images'])
        {
            $images=explode(',',$info['images']);
            foreach ($images as $key => &$value) {
                # code...
                $value=gethost().$value;
            }
        }
        $info['imagelist']=$images;
        //查询清运车辆
        $car=db('car')->where('id','in',$info['car_ids'])->column('plate_number');
        $info['cars']=implode(',',$car);
        $info['store_name']=db('store')->where('id',$info['store_id'])->value('store_name');
        $info['store_faren']=db('store')->where('id',$info['store_id'])->value('name');
        // var_dump($car);
        $change_info=db('change_log')->where(['type'=>'3','change_id'=>$id])->order('id desc')->find();
        $change_images=[];
        $info['edit_time']='';
        if($change_info)
        {
            $change_images=$change_info['change_images'];
            if($change_images)
            {
                $change_images=explode(',',$change_images);
                foreach ($change_images as $key => &$value) {
                    # code...
                    $value=gethost().$value;
                }
            }
            $info['change_title']=$change_info['change_title'];
            $info['edit_time']=date('Y-m-d H:i',$change_info['add_time']);
        }
        $info['change_imagelist']=$change_images;
        return $info;
    }

    public function getAddTimeAttr($value)
    {
        if($value){
            return date("Y-m-d H:i:s",$value);
        }else{
            return "";
        }
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = getstatus();
        return isset($list[$value-1]) ? $list[$value-1] : '';
    }
    
}
